<?php

	/*
		Filename: addgroup.php
		Description: Adds a new group to the database, only accesable for people
		logged in as admin.
		
		Last Edit: 13-08-2012 (dd-mm-yyyy)
	*/
	
	// Inlcude database
	require_once("./Config/Config.php");
	
	// Include functions
	require_once("./Config/Functions.php");
	
	// Check if is admin
	if(isAdmin() == false) die("You are not an administrator.");
	
	// Check if there was a submission
	if(isset($_GET['group_names']))
	{
		// Show Result
		echo '<h1>Group created: ' . $groupname . '</h1>';
		
		echo 'The group named ' . $groupname . ' has been created here are their login codes, required to make the tests.<br><br>';
		
		// Check for empty fields
		if(trim($_GET['group_names']) == "" || trim($_GET['group_name']) == "")
		{
			echo 'Some fields are empty.';
		}
		
		// POST values
		$names = $_GET['group_names'];
		$groupname = mysql_real_escape_string($_GET['group_name']);
		
		// Create group
		$group_id = uniqid("");
		$Result = mysql_query("INSERT INTO groups (group_id, group_name) VALUES ('$group_id', '$groupname');") or die(mysql_error());
		
		echo '<table border="1">';
		echo '<tr>';
		echo '<td><b>UserId</b></td>';
		echo '<td><b>Group</b></td>';
		echo '<td><b>Name</b></td>';
		echo '<td><b>Username</b></td>';
		echo '<td><b>Password</b></td>';
		echo '</tr>';
		
		// Loop trough names 
		$NamesArray = explode("\r\n", $names);
		foreach($NamesArray as $Name)
		{
			$Nam = mysql_real_escape_string($Name);
			$userid = uniqid("");
			$username = uniqid("");
			$pass_plain =uniqid("");
			$password = sha1(md5($pass_plain));
			$adminid = $_SESSION['admin_id'];
			$Result = mysql_query("INSERT INTO users (user_id, group_id, user_name, user_nick, user_password, admin_id) VALUES ('$userid', '$group_id', '$Nam', '$username', '$password', '$adminid');") or die(mysql_error());
			
			echo '<tr>';
			echo "<td>$userid</td>";
			echo "<td>$groupname</td>";
			echo "<td>$Nam</td>";
			echo "<td>$username</td>";
			echo "<td>$pass_plain</td>";
			echo '</tr>';
		}		
		echo '</table>';
		die();
	}	
?>

<h1>Create group</h1>

From this page you can add new groups, just copy and paste a list of names, and a list<br>
of users with password will be created.<br><br>

<form action="./index.php" method="GET">
	<input type="hidden" name="p" value="addgroup">

	<br><b>Group name:</b><br>
	<input type="text" size="110" name="group_name"><br><br>

	<br><b>List of names:</b><br>
	<textarea wrap="physical" cols="80" rows="20" name="group_names"></textarea>
	
	<br><br>
	
	<input type="submit" value="Add">
</form>

